from typing import List


class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        if not nums:
            return 0
        a = [nums[0]]
        ln = len(nums)
        for i in range(1, ln):
            if nums[i] == 1:
                a.append(a[-1]+1)
            else:
                a.append(0)
        b = [nums[-1]]
        for i in range(ln-2, 0, -1):
            if nums[i] == 1:
                b.append(b[-1]+1)
            else:
                b.append(0)
        b.reverse()
        ans = 0
        for i in range(ln):
            ni = 0
            if i > 0:
                ni += a[i-1]
            if i < ln - 1:
                ni += b[i]
            if ni > ans:
                ans = ni
        return ans


solve = Solution()

questions = [
    ([1, 1, 0, 1],),
    ([0, 1, 1, 1, 0, 1, 1, 0, 1],),
    ([1, 1, 0, 1], ),
]

for qi, q in enumerate(questions, 1):
    print(f'#{qi}:', q)
    nums, = q
    print('ans:', solve.longestSubarray(nums))
